package com.leesche.zcsp.node.app.v1.controller.speed;

import com.leesche.zcsp.node.app.v1.controller.register.AppForgetPwdController;
import com.leesche.zcsp.node.app.v1.timer.PhoneCodeCacheTimer;
import com.leesche.zcsp.node.app.v1.timer.VaildPhone;
import com.leesche.zcsp.node.utils.security.QEncodeUtil;
import com.leesche.zcsp.node.utils.web.messge.WebResult;
import com.leesche.zcsp.node.web.constants.Constants;
import com.leesche.zcsp.node.web.model.SysUser;
import com.leesche.zcsp.node.web.model.SysUserCriteria;
import com.leesche.zcsp.node.web.service.PubDeviceService;
import com.leesche.zcsp.node.web.service.PubPlaceService;
import com.leesche.zcsp.node.web.service.SysUserService;
import com.leesche.zcsp.node.web.service.impl.data.read.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dennies yang on 2017-02-08.
 */
@Controller
@RequestMapping(value = "/app/v1/testSpeed")
public class TestSpeedController {

    private static final Logger LOGGER= Logger
            .getLogger(AppForgetPwdController.class);
    private static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    SysUserService sysUserService;

    @Autowired
    PubPlaceService placeService;

    @Autowired
    PubDeviceService deviceService;


    /**
     * <>通过用户名或者手机号 或者商户号查询</>
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "checkUser", method = {RequestMethod.POST,RequestMethod.GET})
    public Map<String, Object> checkUser(QuerySysUserCondition condition) {
        SysUserDto user=null;
        try {
            if(StringUtils.isBlank(condition.getUsername())&&StringUtils.isBlank(condition.getMobile())&&StringUtils.isBlank(condition.getBusinessno())){
                return WebResult.failure("请输入用户名或者手机号或者商户号!").toMap();
            }
            user=sysUserService.findByCondition(condition);
            if(user==null){
                return WebResult.failure("该用户不存在!").toMap();
            }
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("user", user);
            return WebResult.success().put("data",map).toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(Constants.SYSTEM_ERROR_PROMPT_MSG).toMap();
        }
    }

    /**
     * <>通过用户查询场地</>
     * @param condition
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/placeList", method = RequestMethod.POST)
    public Map<String, Object> placeList(QueryPubPlaceCondition  condition) {
        try {
//            Long adminId = 1L;
            if(condition.getAdminId()==null){
                return WebResult.failure("接口参数不正确").toMap();
            }
            List<PubPlaceDto> list = placeService.findPubPlaceList(condition);
            return WebResult.success().put("data",list).toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * <>通过场地查询设备</>
     * @param condition
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deviceList", method = RequestMethod.POST)
    public Map<String, Object> deviceList(QueryPubDeviceCondition  condition) {
        try {
//            Long adminId = 1L;
            List<InfoDeviceDto> list = deviceService.findDeviceList(condition);
            return WebResult.success().put("data",list).toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }
    /**
     * <>统计数据</>
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "deviceCountList", method = RequestMethod.POST)
    public Map<String, Object> deviceList(String start,String end,Long placeId,Long deviceId,Long templateId) {
        try {
            QueryDeviceCountCondition condition=new QueryDeviceCountCondition();
            Date startDate=formatter.parse(start);
            Date endDate=formatter.parse(end);
            condition.setPlaceID(placeId);
            condition.setDeviceID(deviceId);
            condition.setTemplateID(templateId);
            condition.setStartTime(startDate);
            condition.setEndTime(endDate);
            return WebResult.success().put("data",deviceService.countDeviceForFactory(condition)).toMap();
        }catch (ParseException e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure("日期格式不正确").toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * <>统计数据</>
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deviceCountDateList", method = RequestMethod.POST)
    public Map<String, Object> deviceList(String start,String end,Long deviceId) {
        try {
            QueryDeviceCountCondition condition=new QueryDeviceCountCondition();
            Date startDate=formatter.parse(start);
            Date endDate=formatter.parse(end);
            condition.setDeviceID(deviceId);
            condition.setStartTime(startDate);
            condition.setEndTime(endDate);
            return WebResult.success().put("data",deviceService.findDeviceDataForFactory(condition)).toMap();
        } catch (ParseException e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure("日期格式不正确").toMap();
        }catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }






}
